#include<iostream>
#include<sys/types.h>
#include<unistd.h>
#include<string>


int main()
{
    int pfd[2]={0};
    pipe(pfd);

    int id=fork();
    if(id==0)
    {
        //子进程执行
        close(pfd[0]);
        std::string mesg("hello parent process,hello ");
        mesg+=std::to_string(getppid());

        sleep(2);
        write(pfd[1],mesg.c_str(),mesg.size());
        exit(0);
    }
    else if(id>0)
    {//父进程执行
        close(pfd[1]);
        char buf[1024];
        read(pfd[0],buf,1024);
        std::cout<<buf<<std::endl;
    }
    return 0;
}